package com.bluedon.ism.commons.operationlog.service.impl;

import com.bluedon.ism.commons.entity.Account;
import com.bluedon.ism.commons.operationlog.dao.OperatelogDAO;
import com.bluedon.ism.commons.operationlog.entity.Operatelog;
import com.bluedon.ism.commons.operationlog.entity.OperatelogCriteria;
import com.bluedon.ism.commons.operationlog.service.OperatelogService;
import com.bluedon.ism.commons.service.ConfigService;
import com.bluedon.ism.commons.util.PagedQueryResult;
import com.bluedon.ism.commons.util.Pager;
import com.bluedon.ism.commons.util.StringUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**   
 *    
 * 日志操作实现
 */

@Service(value="operatelogService")
public class OperatelogServiceImpl implements OperatelogService {

    private static final Log logger = LogFactory.getLog(OperatelogServiceImpl.class);

    @Autowired
	private  OperatelogDAO operatelogDAO;

	@Override
    @Async
	public void saveLog(Operatelog operateLog) {
        operatelogDAO.save(operateLog);
	}

	@Override
	public Operatelog findOperatelogById(Serializable id) {
		return null;
	}

	@Override
	public Pager findOperatelogAll(Integer start, Integer end) {
		return operatelogDAO.findOperatelogAll(start, end);
	}

	@Override
	public PagedQueryResult findOperatelogPage(Integer pageNo,
			Integer pageSize, OperatelogCriteria criteria) {
		return operatelogDAO.findOperatelogPage(pageNo, pageSize, criteria);
	}

	@Override
	public Pager finOperaterLogPager(Map param, Integer pageNo, Integer pageSize) {
		return operatelogDAO.findOperatelogPage(param,pageNo, pageSize);
	}

    @Override
    public int delLogs(String fns, String startTime, String endTime, Account account){
        Map<String,Object> param = new HashMap<String, Object>();
        int count = 0;
        String bakMsg = "";
        if(StringUtil.isNotBlank(fns)){
            fns = StringUtil.getSQLInValues(fns);
            param.put("fns", fns);
            bakMsg = "手动筛选删除";
        }
        if(StringUtil.isNotBlank(startTime)){
            param.put("startTime", startTime);
            bakMsg = "删除"+startTime;
            if(StringUtil.isBlank(endTime))
                bakMsg+="之后的日志数据";
        }
        if(StringUtil.isNotBlank(endTime)){
            if(endTime.indexOf(":")==-1)
                endTime+=" 23:59:59";
            param.put("endTime", endTime);
            if(StringUtil.isNotBlank(startTime))
                bakMsg+="到"+endTime+"之间的日志数据";
            else
                bakMsg="删除"+endTime+"之前的日志数据";
        }
        if(StringUtil.isBlank(fns)&&StringUtil.isBlank(startTime)&&StringUtil.isBlank(endTime)) {
            logger.error("不能不选择日期或选择记录就删除LOG");
            return 0;
        }

        count = operatelogDAO.delLogs(param);
        logger.warn(account.getRealname() + "  " + bakMsg);
        return count;
    }

}
